package com.hydl.power.service.impl;

import com.hydl.power.mapper.ElectricityFeesMapper;
import com.hydl.power.mapper.PowerdataMapper;
import com.hydl.power.mapper.ProjectMapper;
import com.hydl.power.pojo.ElectricityBill;
import com.hydl.power.pojo.ElectricityFees;
import com.hydl.power.pojo.Project;
import com.hydl.power.service.ElectricityFeesService;
import com.hydl.power.utils.ElectricityFeesUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ElectricityFeesServiceImpl implements ElectricityFeesService {

    @Autowired
    private ElectricityFeesMapper electricityFeesMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private PowerdataMapper powerdataMapper;



    /**
     * ---查询单个变压器每月电费/总电费
     * @param projectId
     * @param deviceId
     * @param time
     * @return
     */
    @Override
    public Map<String,Object> getTransformerElectricityBill(String projectId,String deviceId,String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            Map<String,Object> map=new HashMap<>();
            List<ElectricityBill> list = powerdataMapper.getElectricityBill(deviceId,time);
            ElectricityBill electricity=new ElectricityBill();
            double number=0;
            if (list.size()>0){
                for(int i=0;i<list.size();i++){
                    ElectricityBill electricityBill = list.get(i);
                    if (i+1<list.size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(electricityBill.getForwardActiveTotalElectricalEnergy()*electricityFees.getPrice());
                        double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice();
                        electricity.setDeviceId(electricityBill.getDeviceId());
                        electricity.setDeviceName(electricityBill.getDeviceName());
                        electricity.setTime(time);
                        number += v;
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(electricityBill.getForwardActiveTotalElectricalEnergy()*electricityFees.getPrice());
                        double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice();
                        electricity.setDeviceId(electricityBill.getDeviceId());
                        electricity.setDeviceName(electricityBill.getDeviceName());
                        electricity.setTime(time);
                        number += v;
                    }
                }
                electricity.setPrice(number);
                map.put("list",list);
                map.put("totalPrice",electricity);
                return map;
            }
        }
        return null;
    }


    /**
     * ---查询项目每月电费/总电费
     * @param projectId
     * @param time
     * @return
     */
    @Override
    public Map<String, Object> getElectricityBillProject(String projectId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityBillByProjectId(projectId,time);
            if (list.size()>0){
                Map<String, List<ElectricityBill>> map = new HashMap<>();
                for (ElectricityBill electricityBill : list){
                    if (map.containsKey(electricityBill.getDeviceId().toString())){
                        map.get(electricityBill.getDeviceId().toString()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        map.put(electricityBill.getDeviceId().toString(), newList);
                    }
                }

                List<ElectricityBill> electricityList=new ArrayList<>();
                for(Map.Entry<String, List<ElectricityBill>> entry : map.entrySet()){
                    entry.getValue().sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));
                    for(int i=0;i<entry.getValue().size();i++){
                        ElectricityBill electricityBill = entry.getValue().get(i);
                        if (i+1<entry.getValue().size()){
                            electricityBill.setForwardActiveTotalElectricalEnergy(entry.getValue().get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        }else{
                            electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        }
                    }
                    electricityList.addAll(entry.getValue());
                }

                Map<String, List<ElectricityBill>> electricityBillMap = new HashMap<>();
                for (ElectricityBill electricityBill : electricityList){
                    if (electricityBillMap.containsKey(electricityBill.getTime())){
                        electricityBillMap.get(electricityBill.getTime()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        electricityBillMap.put(electricityBill.getTime(), newList);
                    }
                }

                Map<String, Object> resultMap=new HashMap<>();
                List<ElectricityBill> electricityBillList=new ArrayList<>();
                double totalPrice=0;
                ElectricityBill resultElectricityBill=new ElectricityBill();
                for(Map.Entry<String, List<ElectricityBill>> entry : electricityBillMap.entrySet()){
                    ElectricityBill electricity=new ElectricityBill();
                    double priceNumber=0;
                    List<ElectricityBill> electricityBills = entry.getValue();
                    for (ElectricityBill electricityBill:electricityBills ){
                        electricity.setTime(electricityBill.getTime());
                        double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice();
                        priceNumber += v;
                        totalPrice += v;
                        resultElectricityBill.setDeviceName(electricityBill.getDeviceName());
                        resultElectricityBill.setTime(electricityBill.getTime());
                    }
                    electricity.setPrice(priceNumber);
                    electricityBillList.add(electricity);
                }

                resultElectricityBill.setPrice(totalPrice);

                electricityBillList.sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));

                resultMap.put("list",electricityBillList);
                resultMap.put("totalPrice",resultElectricityBill);
                return resultMap;
                //return electricityBillList;
            }

        }

        return null;
    }


    /**
     * ---查询单个变压器每月力调电费/总力调电费
     * @param projectId
     * @param deviceId
     * @param time
     * @return
     */
    @Override
    public Map<String, Object> getTransformerElectricityFees(String projectId, String deviceId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null) {
            List<ElectricityBill> list = powerdataMapper.getElectricityFees(deviceId, time);
            if (list.size() > 0) {
                Map<String,Object> map=new HashMap<>();
                ElectricityBill resultElectricityBill=new ElectricityBill();
                double totalPrice=0;
                for (int i = 0; i < list.size(); i++) {
                    ElectricityBill electricityBill = list.get(i);
                    if (i + 1 < list.size()) {
                        electricityBill.setForwardActiveTotalElectricalEnergy(list.get(i + 1).getMinForwardActiveTotalElectricalEnergy() - electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setTotalReverseActiveEnergy(list.get(i + 1).getMinTotalReverseActiveEnergy() - electricityBill.getMinTotalReverseActiveEnergy());
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(price);
                        resultElectricityBill.setDeviceId(electricityBill.getDeviceId());
                        resultElectricityBill.setDeviceName(electricityBill.getDeviceName());
                        resultElectricityBill.setTime(time);
                        totalPrice += price;
                    } else {
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy() - electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy() - electricityBill.getMinTotalReverseActiveEnergy());
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(price);

                        resultElectricityBill.setDeviceId(electricityBill.getDeviceId());
                        resultElectricityBill.setDeviceName(electricityBill.getDeviceName());
                        resultElectricityBill.setTime(time);
                        totalPrice += price;
                    }
                }
                resultElectricityBill.setPrice(totalPrice);
                map.put("list",list);
                map.put("totalPrice",resultElectricityBill);
                return map;
            }
        }

        return null;
    }


    /**
     * ---查询项目每月力调电费/总力调电费
     * @param projectId
     * @param time
     * @return
     */
    @Override
    public Map<String, Object> getElectricityFeesProject(String projectId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityFeesByProjectId(projectId,time);
            if (list.size()>0){

                Map<String, Object> resultMap=new HashMap<>();
                Map<String, List<ElectricityBill>> map = new HashMap<>();
                for (ElectricityBill electricityBill : list){
                    if (map.containsKey(electricityBill.getDeviceId().toString())){
                        map.get(electricityBill.getDeviceId().toString()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        map.put(electricityBill.getDeviceId().toString(), newList);
                    }
                }

                List<ElectricityBill> electricityList=new ArrayList<>();
                for(Map.Entry<String, List<ElectricityBill>> entry : map.entrySet()){
                    entry.getValue().sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));
                    for(int i=0;i<entry.getValue().size();i++){
                        ElectricityBill electricityBill = entry.getValue().get(i);
                        if (i+1<entry.getValue().size()){
                            electricityBill.setForwardActiveTotalElectricalEnergy(entry.getValue().get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                            electricityBill.setTotalReverseActiveEnergy(entry.getValue().get(i+1).getMinTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        }else{
                            electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                            electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        }
                    }
                    electricityList.addAll(entry.getValue());
                }

                Map<String, List<ElectricityBill>> electricityBillMap = new HashMap<>();
                for (ElectricityBill electricityBill : electricityList){
                    if (electricityBillMap.containsKey(electricityBill.getTime())){
                        electricityBillMap.get(electricityBill.getTime()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        electricityBillMap.put(electricityBill.getTime(), newList);
                    }
                }

                ElectricityBill resultElectricityBill=new ElectricityBill();
                double totalPrice=0;
                List<ElectricityBill> electricityBillList=new ArrayList<>();
                for(Map.Entry<String, List<ElectricityBill>> entry : electricityBillMap.entrySet()){
                    ElectricityBill electricity=new ElectricityBill();
                    double priceNumber=0;
                    List<ElectricityBill> electricityBills = entry.getValue();
                    for (ElectricityBill electricityBill:electricityBills ){
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricity.setTime(electricityBill.getTime());
                        priceNumber += price;
                        totalPrice+=price;
                        resultElectricityBill.setTime(electricityBill.getTime());
                        resultElectricityBill.setDeviceName(electricityBill.getDeviceName());
                    }
                    electricity.setPrice(priceNumber);
                    electricityBillList.add(electricity);
                }
                resultElectricityBill.setPrice(totalPrice);
                electricityBillList.sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));


                resultMap.put("list",electricityBillList);
                resultMap.put("totalPrice",resultElectricityBill);
                return resultMap;
            }

        }

        return null;
    }


    /**
     * 查询单个变压器每月电费
     * @param projectId
     * @param deviceId
     * @param time
     * @return
     */
    @Override
    public List<ElectricityBill> getMonthElectricityBill(String projectId,String deviceId,String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityBill(deviceId,time);
            if (list.size()>0){
                for(int i=0;i<list.size();i++){
                    ElectricityBill electricityBill = list.get(i);
                    if (i+1<list.size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(electricityBill.getForwardActiveTotalElectricalEnergy()*electricityFees.getPrice());
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(electricityBill.getForwardActiveTotalElectricalEnergy()*electricityFees.getPrice());
                    }
                }
            }
            return list;
        }
        return null;
    }

    /**
     * 查询单个变压器力调电费
     * @param projectId
     * @param deviceId
     * @param time
     * @return
     */
    @Override
    public List<ElectricityBill> getMonthElectricityFees(String projectId, String deviceId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityFees(deviceId,time);
            if (list.size()>0){
                for(int i=0;i<list.size();i++){
                    ElectricityBill electricityBill = list.get(i);
                    if (i+1<list.size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setTotalReverseActiveEnergy(list.get(i+1).getMinTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(price);
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricityBill.setPrice(price);
                    }
                }
            }
            return list;
        }
        return null;
    }

    /**
     * 查询单个变压器一年电费
     * @param projectId 项目id
     * @param deviceId  设备id
     * @param time       年份
     * @return
     */
    @Override
    public ElectricityBill getElectricityBill(String projectId, String deviceId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityBill(deviceId,time);
            ElectricityBill electricity=new ElectricityBill();
            double number=0;
            if (list.size()>0){
                for(int i=0;i<list.size();i++){
                    ElectricityBill electricityBill = list.get(i);
                    if (i+1<list.size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice();
                        electricity.setDeviceId(electricityBill.getDeviceId());
                        electricity.setDeviceName(electricityBill.getDeviceName());
                        electricity.setTime(time);
                        number += v;
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice();
                        electricity.setDeviceId(electricityBill.getDeviceId());
                        electricity.setDeviceName(electricityBill.getDeviceName());
                        electricity.setTime(time);
                        number += v;
                    }
                }

            }
            electricity.setPrice(number);
            return electricity;
        }
       return null;

    }


    /**
     * 查询项目每月总电费电费
     * @param projectId
     * @param time
     */
    @Override
    public List<ElectricityBill> getProjectMonthElectricityBill(String projectId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityBillByProjectId(projectId,time);


            Map<String, List<ElectricityBill>> map = new HashMap<>();
            for (ElectricityBill electricityBill : list){
                if (map.containsKey(electricityBill.getDeviceId())){
                    map.get(electricityBill.getDeviceId()).add(electricityBill);
                }else{
                    List<ElectricityBill> newList = new ArrayList<>();
                    newList.add(electricityBill);
                    map.put(electricityBill.getDeviceId(), newList);
                }
            }

            List<ElectricityBill> electricityList=new ArrayList<>();
            for(Map.Entry<String, List<ElectricityBill>> entry : map.entrySet()){
                entry.getValue().sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));
                for(int i=0;i<entry.getValue().size();i++){
                    ElectricityBill electricityBill = entry.getValue().get(i);
                    if (i+1<entry.getValue().size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(entry.getValue().get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                    }
                }
                electricityList.addAll(entry.getValue());
            }

            Map<String, List<ElectricityBill>> electricityBillMap = new HashMap<>();
            for (ElectricityBill electricityBill : electricityList){
                if (electricityBillMap.containsKey(electricityBill.getTime())){
                    electricityBillMap.get(electricityBill.getTime()).add(electricityBill);
                }else{
                    List<ElectricityBill> newList = new ArrayList<>();
                    newList.add(electricityBill);
                    electricityBillMap.put(electricityBill.getTime(), newList);
                }
            }

            List<ElectricityBill> electricityBillList=new ArrayList<>();
            for(Map.Entry<String, List<ElectricityBill>> entry : electricityBillMap.entrySet()){
                ElectricityBill electricity=new ElectricityBill();
                double priceNumber=0;
                List<ElectricityBill> electricityBills = entry.getValue();
                for (ElectricityBill electricityBill:electricityBills ){
                    electricity.setTime(electricityBill.getTime());
                    double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice();
                    priceNumber += v;
                }
                electricity.setPrice(priceNumber);
                electricityBillList.add(electricity);
            }
            electricityBillList.sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));

            return electricityBillList;
        }
        return null;

    }


    /**
     * 查询项目每月力调电费
     * @param projectId
     * @param time
     */
    @Override
    public List<ElectricityBill> getProjectMonthElectricityFees(String projectId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityFeesByProjectId(projectId,time);
            if (list.size()>0){

                Map<String, List<ElectricityBill>> map = new HashMap<>();
                for (ElectricityBill electricityBill : list){
                    if (map.containsKey(electricityBill.getDeviceId())){
                        map.get(electricityBill.getDeviceId()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        map.put(electricityBill.getDeviceId(), newList);
                    }
                }

                List<ElectricityBill> electricityList=new ArrayList<>();
                for(Map.Entry<String, List<ElectricityBill>> entry : map.entrySet()){
                    entry.getValue().sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));
                    for(int i=0;i<entry.getValue().size();i++){
                        ElectricityBill electricityBill = entry.getValue().get(i);
                        if (i+1<entry.getValue().size()){
                            electricityBill.setForwardActiveTotalElectricalEnergy(entry.getValue().get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                            electricityBill.setTotalReverseActiveEnergy(entry.getValue().get(i+1).getMinTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        }else{
                            electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                            electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        }
                    }
                    electricityList.addAll(entry.getValue());
                }

                Map<String, List<ElectricityBill>> electricityBillMap = new HashMap<>();
                for (ElectricityBill electricityBill : electricityList){
                    if (electricityBillMap.containsKey(electricityBill.getTime())){
                        electricityBillMap.get(electricityBill.getTime()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        electricityBillMap.put(electricityBill.getTime(), newList);
                    }
                }

                List<ElectricityBill> electricityBillList=new ArrayList<>();
                for(Map.Entry<String, List<ElectricityBill>> entry : electricityBillMap.entrySet()){
                    ElectricityBill electricity=new ElectricityBill();
                    double priceNumber=0;
                    List<ElectricityBill> electricityBills = entry.getValue();
                    for (ElectricityBill electricityBill:electricityBills ){
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricity.setTime(electricityBill.getTime());
                        priceNumber += price;
                    }
                    electricity.setPrice(priceNumber);
                    electricityBillList.add(electricity);
                }
                electricityBillList.sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));

                return electricityBillList;
            }

        }

        return null;
    }


    /**
     * 查询项目一年总电费
     * @param projectId 项目id
     * @param time      年份
     * @return
     */
    @Override
    public ElectricityBill getProjectElectricityBill(String projectId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityBillByProjectId(projectId,time);

            Map<String, List<ElectricityBill>> map = new HashMap<>();
            for (ElectricityBill electricityBill : list){
                if (map.containsKey(electricityBill.getDeviceId())){
                    map.get(electricityBill.getDeviceId()).add(electricityBill);
                }else{
                    List<ElectricityBill> newList = new ArrayList<>();
                    newList.add(electricityBill);
                    map.put(electricityBill.getDeviceId(), newList);
                }
            }

            List<ElectricityBill> electricityList=new ArrayList<>();
            for(Map.Entry<String, List<ElectricityBill>> entry : map.entrySet()){
                entry.getValue().sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));
                for(int i=0;i<entry.getValue().size();i++){
                    ElectricityBill electricityBill = entry.getValue().get(i);
                    if (i+1<entry.getValue().size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(entry.getValue().get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                    }
                }
                electricityList.addAll(entry.getValue());
            }
            ElectricityBill electricity=new ElectricityBill();
            double number=0;
            for(ElectricityBill electricityBill:electricityList){
                double v = electricityBill.getForwardActiveTotalElectricalEnergy() * electricityFees.getPrice() ;
                electricity.setTime(time);
                number += v;
            }
            electricity.setPrice(number);
            return electricity;
        }
        return null;
    }

    /**
     * 查询单个变压器一年总力调电费
     * @param projectId
     * @param deviceId
     * @param time
     * @return
     */
    @Override
    public ElectricityBill getElectricityFees(String projectId, String deviceId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityFees(deviceId,time);
            ElectricityBill electricity=new ElectricityBill();
            double number=0;
            if (list.size()>0){
                for(int i=0;i<list.size();i++){
                    ElectricityBill electricityBill = list.get(i);
                    if (i+1<list.size()){
                        electricityBill.setForwardActiveTotalElectricalEnergy(list.get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setTotalReverseActiveEnergy(list.get(i+1).getMinTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricity.setDeviceId(electricityBill.getDeviceId());
                        electricity.setDeviceName(electricityBill.getDeviceName());
                        electricity.setTime(time);
                        number += price;
                    }else{
                        electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                        electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        //计算平均功率因数
                        double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                        //计算力调电费
                        double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(), electricityBill.getForwardActiveTotalElectricalEnergy());
                        electricity.setDeviceId(electricityBill.getDeviceId());
                        electricity.setDeviceName(electricityBill.getDeviceName());
                        electricity.setTime(time);
                        number += price;
                    }
                }
            }
            electricity.setPrice(number);
            return electricity;
        }
        return null;

    }


    /**
     * 查询项目一年总力调电费
     * @param projectId
     * @param time
     * @return
     */
    @Override
    public ElectricityBill getProjectElectricityFees(String projectId, String time) {
        Project project = projectMapper.queryProjectByProId(projectId);
        ElectricityFees electricityFees = electricityFeesMapper.getElectricityFeesByTime(project.getElectricityFeesType(), time);
        if (electricityFees!=null){
            List<ElectricityBill> list = powerdataMapper.getElectricityFeesByProjectId(projectId,time);
            if (list.size()>0){
                Map<String, List<ElectricityBill>> map = new HashMap<>();
                for (ElectricityBill electricityBill : list){
                    if (map.containsKey(electricityBill.getDeviceId())){
                        map.get(electricityBill.getDeviceId()).add(electricityBill);
                    }else{
                        List<ElectricityBill> newList = new ArrayList<>();
                        newList.add(electricityBill);
                        map.put(electricityBill.getDeviceId(), newList);
                    }
                }
                List<ElectricityBill> electricityList=new ArrayList<>();
                for(Map.Entry<String, List<ElectricityBill>> entry : map.entrySet()){
                    entry.getValue().sort((t1, t2) -> t1.getTime().compareTo(t2.getTime()));
                    for(int i=0;i<entry.getValue().size();i++){
                        ElectricityBill electricityBill = entry.getValue().get(i);
                        if (i+1<entry.getValue().size()){
                            electricityBill.setForwardActiveTotalElectricalEnergy(entry.getValue().get(i+1).getMinForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                            electricityBill.setTotalReverseActiveEnergy(list.get(i+1).getMinTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        }else{
                            electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                            electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                        }
                    }
                    electricityList.addAll(entry.getValue());
                }

                ElectricityBill electricity=new ElectricityBill();
                double number=0;
                for (ElectricityBill electricityBill:electricityList ){
                    electricityBill.setForwardActiveTotalElectricalEnergy(electricityBill.getMaxForwardActiveTotalElectricalEnergy()-electricityBill.getMinForwardActiveTotalElectricalEnergy());
                    electricityBill.setTotalReverseActiveEnergy(electricityBill.getMaxTotalReverseActiveEnergy()-electricityBill.getMinTotalReverseActiveEnergy());
                    //计算平均功率因数
                    double averagePowerFactor = ElectricityFeesUtils.getAveragePowerFactor(electricityBill.getForwardActiveTotalElectricalEnergy(), electricityBill.getTotalReverseActiveEnergy());
                    //计算力调电费
                    double price = ElectricityFeesUtils.getElectricityFees(averagePowerFactor, electricityFees.getPrice(),electricityBill.getForwardActiveTotalElectricalEnergy());
                    electricity.setTime(time);
                    number += price;
                }
                electricity.setPrice(number);
                return electricity;
            }
        }
        return null;
    }


}
